angular.module('Controller')
.controller('WFAccountBindRoleCtrl', ($scope,NgTableParams,wfAlert,items,$uibModalInstance,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.chkBox={ids:{},disabled:{},data:{}}

    initSearchData = ()->
        $scope.search = {
            enabled:true
        }
    initSearchData()
    loadData=()->
        _.each items.data,(item)->
            $scope.chkBox.ids[item.id]=true
            $scope.chkBox.disabled[item.id]=true
            $scope.chkBox.data[item.id]=item
    loadData()
    $scope.typeList=commonConstant.authorizationTypeList(true)
    $scope.enabledList=commonConstant.enabledList()
    $scope.sexList=commonConstant.sexList()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count(),
            nameLike: $scope.search.name,
            type: $scope.search.type,
            enabled:true,
            accountId:$localStorage['accountId']
        }
        getParam=commonUtil.transferGetParam(requestData)
        $http.get('@ostengar/role'+getParam)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)

    $scope.queryData=(firstPage)->
        if(firstPage == true)
            $scope.tableParams.page(1)
        $scope.tableParams.reload()

    $scope.pickData=(item)->
        if $scope.chkBox.data[item.id]==undefined
            $scope.chkBox.data[item.id]=item
    $scope.confirm=()->
        list=[]
        ids=''
        _.each $scope.chkBox.ids,(val,id)->
            if val==true and $scope.chkBox.data[id]!=undefined and $scope.chkBox.disabled[id]!=true
                list.push $scope.chkBox.data[id]
                ids+=id+','
        if ids.length>0
            ids=ids.substring(0,ids.length-1)
            $http.post('@ostengar/adminUser/'+items.roleID+'/roles?roles='+ids)
            .success((resp)->
                console.log('绑定角色权限：',resp)
                if(resp.code==0)
                    wfAlert.toastInfo('绑定角色权限成功')
                    $uibModalInstance.close()
            )
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)